package club.xinghe.biz;


import club.xinghe.dao.TkDao;
import club.xinghe.entity.Tk;
import club.xinghe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明:对试题进行各种操作的业务
 *
 * @姚文斌
 * @create 2019-09-28 下午 6:24
 */
public class TkBiz {
    /**
     * 向题库中添加试题
     * 增删改要提交
     * session 每次使用过之后一定要关闭释放资源，否则会拖慢查询效率
     * @param tk
     * @return
     */
    /**
     * 向题库中添加试题
     * 增删改要提交
     * session 每次使用过之后一定要关闭释放资源，否则会拖慢查询效率
     * @param tk
     * @return
     */
    public int addTk(Tk tk){

        //获取session
        SqlSession session= SqlSessionFactoryUtil.getSqlSession();
        int result=0;
        try{
            TkDao tkDao=session.getMapper(TkDao.class);
            result=tkDao.addTk(tk);
            session.commit();
        }finally {
            session.close();
        }
        return result;
    }

    /**
     * 从题库删除试题
     * @param tId
     * @return
     */
    public int delTk(int tId){

        //获取session
        SqlSession session= SqlSessionFactoryUtil.getSqlSession();
        int result=0;
        try{
            TkDao tkDao=session.getMapper(TkDao.class);
            result=tkDao.delTk(tId);
            session.commit();
        }finally {
            session.close();
        }
        return result;
    }

    /**
     * 修改题库中的试题
     * @param tk
     * @return
     */
    public int editTk(Tk tk){

        //获取session
        SqlSession session= SqlSessionFactoryUtil.getSqlSession();
        int result=0;
        try{
            TkDao tkDao=session.getMapper(TkDao.class);
            result=tkDao.editTk(tk);
            session.commit();
        }finally {
            session.close();
        }
        return result;
    }

    /**
     * 取出题库中所有试题,包含分页,关键字搜索和单独查询题型
     * @return
     */
    public List<Tk> showTk(int pageIndex,int pageSize,int styleId,int zId,String keyword){

        Map map=new HashMap();
        map.put("pageIndex",pageSize*(pageIndex-1));
        map.put("pageSize",pageSize);
        map.put("styleId",styleId);
        map.put("zId",zId);
        map.put("keyword",keyword);
        //获取session
        SqlSession session= SqlSessionFactoryUtil.getSqlSession();
        List<Tk> tkList=null;
        try{
            TkDao tkDao=session.getMapper(TkDao.class);
            tkList=tkDao.showTk(map);
        }finally {
            session.close();
        }
        return tkList;
    }

    /**
     * 获取单个试题类型总数
     * @return
     */
    public int getSingleCount(int zId,int styleId){
        //获取session
        SqlSession session= SqlSessionFactoryUtil.getSqlSession();
        Map map=new HashMap();
        map.put("zId",zId);
        map.put("styleId",styleId);
        int count=0;
        try{
            TkDao tkDao=session.getMapper(TkDao.class);
            count=tkDao.getSingleCount(map);
        }finally {
            session.close();
        }
        return count;

    }

    /**
     * 如果是关键字搜索使用这个方法统计总条数
     * @return
     */
    public int getCountByKeyword(int styleId,int zId,String keyword){
        //获取session
        SqlSession session= SqlSessionFactoryUtil.getSqlSession();
        Map map=new HashMap();
        map.put("styleId",styleId);
        map.put("zId",zId);
        map.put("keyword",keyword);
        int count=0;
        try{
            TkDao tkDao=session.getMapper(TkDao.class);
            count=tkDao.getCountByKeyword(map);
        }finally {
            session.close();
        }
        return count;
    }





    /**
     * 说明：根据卷库的试题字符串查询试题
     * @姚家乐
     * @return
     */
    public List<Tk> showEtk(String estring) {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        List<Tk> tkList = new ArrayList<>();
        try {
            TkDao tkDao = session.getMapper(TkDao.class);
            if (estring.length()>0){
                String[] tkcs = estring.split(",");
                for (int i = 0; i < tkcs.length ; i++) {
                    Tk tk = tkDao.showEtk(Integer.parseInt(tkcs[i]));
                    tkList.add(tk);
                }
            }else if (estring==""){
                Tk tk = tkDao.showEtk(0);
                tkList.add(tk);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return tkList;
    }

    /**
     * 说明：根据专业的id查询试题加上分页
     * 姚家乐
     * @param ezid
     * @return
     */
    public List<Tk> showUpEtk(int pageNum,int pageSize,int ezid,int stid){
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        List<Tk> tkList = new ArrayList<>();
        try {
            TkDao tkDao = session.getMapper(TkDao.class);
            Map<String,Object> map = new HashMap<>();
            map.put("pageS",pageSize*(pageNum-1));
            map.put("pageSize",pageSize);
            map.put("ezid",ezid);
            map.put("stid",stid);
            tkList = tkDao.showUpEtk(map);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return tkList;
    }
    /**
     * 说明：根据专业和题型查询试题数量
     * 姚家乐
     * @param ezid,stid
     */
    public int tkCount(int ezid,int stid){
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        int n = 0;
        try {
            TkDao tkDao = session.getMapper(TkDao.class);
            Map map = new HashMap();
            map.put("ezid",ezid);
            map.put("stid",stid);
            n = tkDao.tkCount(map);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            session.close();
        }
        return n;
    }
}
